package hu.blok1.veilingsite.client.gui;

import hu.blok1.veilingsite.client.Veilingsite;
import hu.blok1.veilingsite.client.data.AccountService;
import hu.blok1.veilingsite.client.data.AccountServiceAsync;
import hu.blok1.veilingsite.shared.User;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class loginGUI {
	private VerticalPanel vp, secondvp;

	private Button b_login, b_clear;
	private TextBox tf_user;
	private PasswordTextBox tf_pass;
	private Grid grid;

	private Veilingsite veiling;

	private final AccountServiceAsync accountService = GWT
			.create(AccountService.class);

	public loginGUI(Veilingsite vs) {
		veiling = vs;
		vp = veiling.getMainPanel();
		if (veiling.getUser().isLoggedIn()) {
			init_loggedin();
		} else {
			init_login();
		}
	}

	public void init_login() {
		vp.clear();
		secondvp = new VerticalPanel();
		secondvp.setWidth(100 + "%");
		secondvp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		secondvp.add(new Label("Log in met uw email-adres en uw wachtwoord"));

		tf_user = new TextBox();
		tf_pass = new PasswordTextBox();
		b_login = new Button("Log-in");
		b_clear = new Button("Clear");

		tf_user.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				char keyPress = event.getCharCode();
				if (keyPress == KeyCodes.KEY_ENTER) {
					logIn(tf_user.getText(), tf_pass.getText());
				}
			}
		});

		tf_pass.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				char keyPress = event.getCharCode();
				if (keyPress == KeyCodes.KEY_ENTER) {
					logIn(tf_user.getText(), tf_pass.getText());
				}
			}
		});

		b_login.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				logIn(tf_user.getText(), tf_pass.getText());
			}
		});

		b_clear.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				clear();
			}
		});

		grid = new Grid(3, 2);
		grid.setWidget(0, 0, new Label("Email-adres:"));
		grid.setWidget(0, 1, tf_user);
		grid.setWidget(1, 0, new Label("Wachtwoord:"));
		grid.setWidget(1, 1, tf_pass);
		grid.setWidget(2, 0, b_login);
		grid.setWidget(2, 1, b_clear);
		secondvp.add(grid);
		vp.add(secondvp);
	}

	public void init_loggedin() {
		vp.clear();
		veiling.reloadHeader();
		vp.add(new Label("You are already logged in!"));
		vp.add(new Hyperlink("Log off", "logout"));
	}

	public void logIn(String _user, String _pass) {
		if (!_user.equals("") && !_pass.equals("")) {
			b_login.setEnabled(false);
			User user = new User();
			veiling.setUser(user);
			user.setEmail(_user);
			user.setPassword(_pass);
			accountService.logIn(user, new AsyncCallback<User>() {
				public void onFailure(Throwable caught) {
					Window.alert("Error with the Connection :D");
				}

				public void onSuccess(User result) {
					if (!result.getStatus().equals("invalid")) {
						if (result.getStatus().equals("Active")) {
							veiling.setUser(result);
							veiling.reloadHeader();
							History.newItem("home");
						} else {
							Window.alert("Deze gebruiker heeft geen rechten om in te loggen. Neem contact op met een Administrator.");
						}
					} else {
						Window.alert("Wrong username/password");
						b_login.setEnabled(true);
					}
				}
			});
		} else {
			Window.alert("Wrong username/password");
			b_login.setEnabled(true);
		}
	}

	public void clear() {
		tf_user.setText("");
		tf_pass.setText("");
	}
}